package com.example.ymm.modules1.service.impl.hl.kq;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbUser;
import com.example.ymm.modules1.entity.my.user.KqDept;
import com.example.ymm.modules1.entity.my.user.KqUser;
import com.example.ymm.modules1.mapper.my.user.KqDeptMapper;
import com.example.ymm.modules1.query.kq.KqDeptQueryCriteria;
import com.example.ymm.modules1.service.user.DbUserService;
import com.example.ymm.modules1.service.hl.kq.KqDeptService;
import com.example.ymm.modules1.service.hl.kq.KqUserService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DSTransactional
@DS("slave2")
@Slf4j
public class KqDeptServiceImpl extends BaseServiceImpl<KqDeptMapper, KqDept> implements KqDeptService {

    @Autowired
    KqUserService kqUserService;
    @Autowired
    DbUserService dbUserService;

    @Override
    public TableList<KqDept> list(KqDeptQueryCriteria criteria, YmPageDto pageDto) {
        IPage<KqDept> page = new Page<KqDept>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(KqDept.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<KqDept> tableList=new TableList(page);
        return tableList;
    }

    public List<KqDept> queryAll(KqDeptQueryCriteria criteria) {
        List<KqDept> result =baseMapper.selectList(QueryHelpPlus.getPredicate(KqDept.class, criteria));
        for (KqDept kqDept : result) {
            kqDept.setGroupAndUserName(kqDept.getGroupName()+":"+kqDept.getGroupUserName());
        }
        return result;
    }


    @Override
    public KqDept findByGroupAndManageName(String groupAndManageName) {
        KqDeptQueryCriteria criteria = new KqDeptQueryCriteria();
        criteria.setGroupUserAndManageName(groupAndManageName);
        List<KqDept> kqDepts = queryAll(criteria);
        if(kqDepts.size()>0){
            return kqDepts.get(0);
        }
        return null;
    }

    @Override
    public KqDept findByGroupUserName(String groupUserName) {
        KqDeptQueryCriteria criteria = new KqDeptQueryCriteria();
        criteria.setGroupUserName(groupUserName);
        List<KqDept> kqDepts = queryAll(criteria);
        if(kqDepts.size()>0){
            return kqDepts.get(0);
        }
        return null;
    }



    @Override
    public KqDept findByManageUserName(String manageUserName) {
        KqDeptQueryCriteria criteria = new KqDeptQueryCriteria();
        criteria.setManageUserName(manageUserName);
        List<KqDept> kqDepts = queryAll(criteria);
        if(kqDepts.size()>0){
            return kqDepts.get(0);
        }
        return null;
    }

    /**
     * 钉钉部门调整时 自动调整到车间考勤组
     * @param deptId
     * @param userId
     */
    public void joinDeptId(String deptId,String userId) {
        KqDeptQueryCriteria criteria=new KqDeptQueryCriteria();
        criteria.setDeptId(deptId);
        List<KqDept> kqDepts = queryAll(criteria);
        if(kqDepts.size()>0){
            KqDept kqDept = kqDepts.get(0);
            KqUser kqUser = kqUserService.getById(userId);
            if(UtilValidate.isNotEmpty(kqUser)){
                kqUser.setKqGroupId(kqDept.getId());
                kqUserService.saveOrUpdate(kqUser);
            }else{
                kqUser = new KqUser();
                kqUser.setUserId(userId);
                DbUser dbUser = dbUserService.findByUserId(userId);
                kqUser.setUserName(dbUser.getUserName());
                kqUser.setKqGroupId(kqDept.getId());
                kqUserService.saveOrUpdate(kqUser);
            }

        }
    }


}
